package com.shh.shojbackendjudgeservice.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.shh.shojbackendmodel.model.codesandbox.JudgeInfo;
import com.shh.shojbackendmodel.model.dto.question.JudgeCase;
import com.shh.shojbackendmodel.model.dto.question.JudgeConfig;
import com.shh.shojbackendmodel.model.entity.Question;
import com.shh.shojbackendmodel.model.enums.JudgeInfoMessageEnum;
import com.shh.shojbackendmodel.model.enums.QuestionSubmitStatusEnum;
import lombok.extern.slf4j.Slf4j;


import java.util.List;
import java.util.Optional;

/**
 * java判题策略
 */
@Slf4j
public class JavaLanguageJudgeStrategy implements JudgeStrategy {
    /**
     * 执行判题
     * @param judgeContext
     * @return
     */
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        log.info("开始判题");
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();

        Long memory = Optional.ofNullable(judgeInfo.getMemory()).orElse(0L);// null
        Long time = Optional.ofNullable(judgeInfo.getTime()).orElse(0L);// 222
        Integer status = judgeContext.getStatus();
        List<String> inputList = judgeContext.getInputList();// 1 2
        List<String> outputList = judgeContext.getOutputList();//3
        List<JudgeCase> judgeCasesList = judgeContext.getJudgeCasesList(); //input 1 2  output 3
        Question question = judgeContext.getQuestion();
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMemory(memory);
        judgeInfoResponse.setTime(time);


        //默认返回ACCEPTED
        JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;
        if(status.equals(QuestionSubmitStatusEnum.FAILED.getValue()) || status.equals(QuestionSubmitStatusEnum.COMPILE_FAILED.getValue())){
            judgeInfoMessageEnum = JudgeInfoMessageEnum.COMPILE_ERROR;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            log.info("沙箱代码编译错误或系统错误");
            return judgeInfoResponse;
        }

        //6.判断沙箱执行结果数量是否与输入数量一致
        if(outputList.size() !=inputList.size()){
            judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            log.info("沙箱执行结果数量与输入数量不一致");
            return judgeInfoResponse;
        }
        //判断每一项输出是否与预期输出相等
        for(int i=0;i<judgeCasesList.size();i++){
            JudgeCase judgeCase = judgeCasesList.get(i);
            if(!judgeCase.getOutput().equals(outputList.get(i))){
                log.info("答案错误");
                judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoResponse;
            }
        }
        //判断题目限制
        String judgeConfigStr  = question.getJudgeConfig();
        //这个是反序列化的一个方法 toBean 可以将字符串反序列化为一个java对象
        JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
        Long needMemoryLimit = judgeConfig.getMemoryLimit();
        Long needTimeLimit = judgeConfig.getTimeLimit();
        if(memory > needMemoryLimit){
            log.info("内存超出");
            judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        //假设java需要额外的10秒时间
        final Long extraTime = 10000L;
        if(time - extraTime > needTimeLimit){
            log.info("时间超出");
            judgeInfoMessageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
        return judgeInfoResponse;
    }
}
